﻿using System.Windows;
using ICSharpCode.AvalonEdit;

namespace Poet
{
    /// <summary>
    /// Interaction logic for GoToLineDialog.xaml
    /// </summary>
    public partial class FindAndReplaceDialog
    {
        public TextEditor TxtBox1;
        private string _replacedString;

        public FindAndReplaceDialog()
        {
            InitializeComponent();
        }

        public void Find()
        {
            var child = ((MainWindow)Application.Current.MainWindow).dockingManager.ActiveDocument as ChildWindow;
            if (child == null) return;

            TxtBox1 = child._textEditor;
            var pattern = SearchPatternTextBox.Text;
            ((FindAndRepaceManager)FindSearchSettingsControl.Resources["SearchAndReplaceManager"]).Find(TxtBox1, pattern);
        }

        public void Replace()
        {
            var child = ((MainWindow)Application.Current.MainWindow).dockingManager.ActiveDocument as ChildWindow;
            if (child == null) return;

            TxtBox1 = child._textEditor;
            var pattern = ReplacePatternTextBox.Text;
            var newSubstring = ReplaceComboBox.Text;

            if (_replacedString != TxtBox1.SelectedText)
                _replacedString = string.Empty;

            _replacedString = ((FindAndRepaceManager)ReplaceSearchSettingsControl.Resources["SearchAndReplaceManager"]).
                Replace(TxtBox1, _replacedString, newSubstring, pattern);
        }

        private void FindButtonClick(object sender, RoutedEventArgs e)
        {
            Find();
        }

        private void ReplaceButtonClick(object sender, RoutedEventArgs e)
        {
            Replace();
        }

        private void TabItem1GotFocus(object sender, RoutedEventArgs e)
        {
            //if(!_findWhatTextBox.IsFocused) _findWhatTextBox.Focus();
        }
    }
}
